/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.model.dbgservice {
	import com.googlecode.milib.dbg.view.IMLDBGWindowContentProvider;
	import com.googlecode.milib.dbg.view.MLDBGStage;
	import com.googlecode.milib.dbg.view.MLDBGWindow;
	import com.googlecode.milib.vo.MLEvent;

	/**
	 * @author Marek Brun
	 */
	public class AbstractDBGService implements IMLDBGWindowContentProvider {

		protected var obj:Object;
		private var window:MLDBGWindow;
		private var isNowInWindow:Boolean;

		public function AbstractDBGService(obj:Object) {
			this.obj=obj;
		}
		
		public function openInMLDBGWindow():void {
			if(window){
				openInCurrentMLDBGWindow();
			}else{
				setCurrentMLDBGWindow(MLDBGWindow.forInstance(obj));
				openInCurrentMLDBGWindow();
			}
		}
		
		public function openInCurrentMLDBGWindow():void {
			if(window) {
				MLDBGStage.getInstance().addMLDBGWindow(window);
				window.setContentProvider(this);
				window.setIsSelected(true);
			}else{
				throw new Error('Cant be opened because there is no current window');
			}
		}
		
		public function setCurrentMLDBGWindow(window:MLDBGWindow):void {
			if(this.window){
				this.window.removeEventListener(window.event_NewContentProvider, onWindowNewDBGService);
			}
			this.window=window;
			this.window.addEventListener(window.event_NewContentProvider, onWindowNewDBGService);
		}
		
		/*abstract*/ public function getMLDBGWindowTitle():String {
			throw new Error('Abstract method call. Please write implementation');
		}
		
		/*abstract*/ public function getMLDBGWindowContents():Array {
			throw new Error('Abstract method invoke. Please write implementation');
		}
		
		public function getServicedObject():Object {
			return obj;
		}
		
//********************************************************************************************
//		events for AbstractDBGService
//********************************************************************************************
		protected function onWindowNewDBGService(event:MLEvent):void {
			isNowInWindow=event.info.data==this;
		}
	}
}
